#!/usr/bin/env python3
"""
为BudgetAgent和WeatherAgent添加详细调试日志的工具
"""

import re

def add_debug_logs_to_budget_agent():
    """为BudgetAgent添加详细的调试日志"""
    
    # 要添加的调试日志代码片段
    debug_methods = '''
    def _debug_log_conflict_detection(self, conflicts, user_requirements):
        """调试日志：冲突检测过程"""
        print("\\n" + "="*60)
        print("🐛 [调试] BudgetAgent冲突检测过程")
        print("="*60)
        
        base_budget = self._extract_budget_from_requirements(user_requirements)
        print(f"📊 基础预算: {base_budget}欧元")
        
        print(f"🔍 检测到冲突数量: {len(conflicts)}")
        for i, conflict in enumerate(conflicts, 1):
            print(f"  冲突{i}: {conflict.get('type', 'unknown')}")
            print(f"    严重程度: {conflict.get('severity', 'unknown')}")
            print(f"    描述: {conflict.get('description', 'no description')}")
            if 'amount' in conflict:
                print(f"    影响金额: {conflict['amount']}欧元")
        
        # 判断是否会触发主动协商
        high_severity = [c for c in conflicts if c.get('severity') in ['high', 'critical']]
        if high_severity:
            print(f"🚨 检测到{len(high_severity)}个高严重级别冲突，将触发主动协商!")
        else:
            print(f"ℹ️ 未检测到高严重级别冲突，不会触发主动协商")
        
        print("="*60)
    
    def _debug_log_negotiation_sending(self, agent_type, negotiation_data):
        """调试日志：协商发送过程"""
        print("\\n" + "="*60)
        print("🐛 [调试] BudgetAgent发送协商请求")
        print("="*60)
        
        print(f"🎯 目标Agent: {agent_type}")
        print(f"🌐 目标URL: {self.peer_agents.get(agent_type, 'unknown')}")
        print(f"📋 协商类型: {negotiation_data.get('type', 'unknown')}")
        print(f"📤 发送者: {negotiation_data.get('sender', 'unknown')}")
        
        context = negotiation_data.get('negotiation_context', {})
        print(f"📊 协商上下文:")
        print(f"    问题: {context.get('issue', 'unknown')}")
        print(f"    严重程度: {context.get('severity', 'unknown')}")
        print(f"    请求成本削减: {context.get('requested_cost_reduction', 'unknown')}欧元")
        
        proposals = negotiation_data.get('proposals', [])
        print(f"💡 提案数量: {len(proposals)}")
        for i, proposal in enumerate(proposals, 1):
            print(f"  提案{i}: {proposal.get('option', 'unknown')}")
            print(f"    描述: {proposal.get('description', 'no description')}")
            print(f"    预期节省: {proposal.get('expected_savings', 'unknown')}欧元")
        
        print("🚀 即将发送A2A协商请求...")
        print("="*60)
'''
    
    return debug_methods

def add_debug_logs_to_weather_agent():
    """为WeatherAgent添加详细的调试日志"""
    
    debug_methods = '''
    def _debug_log_negotiation_received(self, request_data):
        """调试日志：收到协商请求"""
        print("\\n" + "="*60)
        print("🐛 [调试] WeatherAgent收到协商请求")
        print("="*60)
        
        print(f"📥 请求类型: {request_data.get('type', 'unknown')}")
        print(f"📤 发送者: {request_data.get('sender', 'unknown')}")
        
        context = request_data.get('negotiation_context', {})
        if context:
            print(f"📊 协商上下文:")
            print(f"    问题: {context.get('issue', 'unknown')}")
            print(f"    严重程度: {context.get('severity', 'unknown')}")
            print(f"    请求成本削减: {context.get('requested_cost_reduction', 'unknown')}欧元")
        
        proposals = request_data.get('proposals', [])
        if proposals:
            print(f"💡 收到提案数量: {len(proposals)}")
            for i, proposal in enumerate(proposals, 1):
                print(f"  提案{i}: {proposal.get('option', 'unknown')}")
                print(f"    描述: {proposal.get('description', 'no description')}")
                print(f"    预期节省: {proposal.get('expected_savings', 'unknown')}欧元")
        
        print("🤔 开始分析协商请求...")
        print("="*60)
    
    def _debug_log_negotiation_response(self, response):
        """调试日志：协商响应"""
        print("\\n" + "="*60)
        print("🐛 [调试] WeatherAgent协商响应")
        print("="*60)
        
        print(f"📤 响应类型: {response.get('type', 'unknown')}")
        print(f"🤖 Agent: {response.get('agent', 'unknown')}")
        
        if 'error' in response:
            print(f"❌ 错误信息: {response['error']}")
        else:
            print(f"✅ 协商处理成功")
            
            if 'negotiation_points' in response:
                points = response['negotiation_points']
                print(f"💡 生成协商建议: {len(points)}个")
                for point in points:
                    print(f"  -> {point.get('target_agent', 'unknown')}: {point.get('topic', 'unknown')}")
            
            if 'potential_conflicts' in response:
                conflicts = response['potential_conflicts']
                print(f"⚠️ 预测冲突: {len(conflicts)}个")
                for conflict in conflicts:
                    print(f"  -> {conflict.get('conflict_type', 'unknown')} ({conflict.get('severity', 'unknown')})")
        
        print("="*60)
'''
    
    return debug_methods

print("📋 调试工具说明:")
print("1. 运行 debug_negotiation.py 来模拟和观察协商过程")
print("2. 查看 negotiation_debug.log 文件获取详细日志")
print("3. 可以在BudgetAgent和WeatherAgent中添加上述调试方法")